package com.beifu.manager.service.user.impl;

import java.util.List;

import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.beifu.manager.dao.ModuleMapper;
import com.beifu.manager.dao.UserInfoMapper;
import com.beifu.manager.model.Module;
import com.beifu.manager.model.UserInfo;
import com.beifu.manager.model.UserInfoExample;
import com.beifu.manager.service.user.UserService;
import com.mds.manager.utils.ListUtils;
import com.mds.manager.utils.PageUtils;

/**
 * Created by 再壮 on 2018/6/10.
 */

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserInfoMapper userInfoMapper;
    
    @Autowired
    private ModuleMapper moduleMapper;

    @Override
	public UserInfo login(String username, String password) {
		UserInfoExample userInfoExample = new UserInfoExample();
		userInfoExample.createCriteria().andLoginNameEqualTo(username);
		
		//根据用户名查找user
		List<UserInfo> list = userInfoMapper.selectByExample(userInfoExample);
		if (!ListUtils.isNotNull(list)) {
			// 因为缓存切面的原因,在这里就抛出用户名不存在的异常
			throw new UnknownAccountException("用户名不存在");
		}/*else if(list.get(0).getStatus() == 0){
            throw new LockedAccountException("用户已经被禁用，请联系管理员启用该账号");
        }*/
		return list.get(0);
	}
    
    /*@Override
    public User getUserInfo(String userName, String password) {
        // TODO 根据用户名查询用户信息返回。
        User user = new User();
        user.setLoginname(userName);
        
        //user.setLoginpassword(passwordUtil.StrToMD5(userName+password));
        
        user.setLoginpassword(new SimpleHash("MD5", password, userName).toString());
        return user;
    }*/

    @Override
    public List<Module> getModuleList(int userId,String roleType,int modulePlatform) {
        return moduleMapper.selectModuleByUserId(userId,roleType,modulePlatform);
    }

    @Override
    public List<UserInfo> getUserInfoList(){
        return userInfoMapper.selectByExample(null);
    }

    @Override
    public List<UserInfo> getUserAndOrganize() {
        return userInfoMapper.selectUserAndOrganize();
    }

    @Override
    public PageUtils getUserPages(PageUtils Page) {
        UserInfoExample example = (UserInfoExample) Page.getQueryParames();
        List<UserInfo> list = userInfoMapper.getUserPages((Page.getPage()-1)*Page.getLimit(),Page.getLimit(), example);
        int count = userInfoMapper.countByExample(example);
        Page.setList(list);
        Page.setCountNum(count);
        return Page;
    }

    @Override
    public int insert(UserInfo record) {
        String password = new SimpleHash("MD5", "123456", record.getLoginName()).toString();
        record.setLoginPassword(password);
        return userInfoMapper.insert(record);
    }

    @Override
    public int updateByPrimaryKey(UserInfo record) {
        return userInfoMapper.updateByPrimaryKey(record);
    }

    @Override
    public int batchDelUser(String[] ids) {
        return userInfoMapper.batchDelUser(ids);
    }
}
